package com.HttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.alibaba.fastjson.JSONObject;

public class HelloServiceTest {

	public static String HELLO_WS_URL = "http://10.86.88.136:8000/PSIGW/PeopleSoftServiceListeningConnector/G_GET_LOCATION.1.wsdl";

	// 调用WS
	private static void testHelloService(String name) throws Exception, IOException {

		String usersig = "eJxNjU9PgzAYxr9LrxjTFrqBiQd0XVzmJLF68dKQtcyGUUppGcz43QXCEt-b*-z5PT-g45Xdt6LkuTFKgAeAIgghQtEKg7vZlL1RVvK8cNJOPiEEj5HFnWs8dzy0U-smO1XJGbaGGMUoiRddCamdKtSM6tv*mopK6RvreKy9dtwNRv5jteo0fgf6*bxL9*yc21NSbTQeDqLL*ovfwKeMehK*B6J7o768boPIJnGqaGqqdeN3YbDHWTN8vWhWF6xML0xmCd1GXS*61XdzFoNx8eMy1knbqlqPgxgignAIpwO-f5p*Wjw_";
		String identifier = "xsxzAdmin";
		long sdkappid = 1400011462;
		String random = getFixLenthString();
		String url1 = "https://console.tim.qq.com/v4/im_open_login_svc/account_import?usersig=" + usersig
				+ "&identifier=" + identifier + "&sdkappid=" + sdkappid + "&random=" + random + "&contenttype=json";

//		String sendMsg = "{\"To_Account\":\"" + 121 + "\"," + "\"MsgRandom\": " + 121 + ","
//				+ "\"MsgTimeStamp\": 5454457," + "\"MsgBody\": [{\"MsgType\":\"TIMCustomElem\","
//				+ "\"MsgContent\": {\"Data\": \"" + 2112 + "\",\"Desc\": \"" + 1212 + "\",\"Ext\": \"" + 1212
//				+ "\",\"Sound\": \"dingdong.aiff\"}}]}";
		Map map=new HashMap<String, String>();
		map.put("Identifier", "identifier_"+10001635);
		map.put("Nick", "销售公司");
		JSONObject json=new JSONObject();
		  String sendMsg = json.toJSONString(map);
		// 开启HTTP连接ַ
		URL url = new URL(url1);
		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

		// 设置HTTP请求相关信息
		httpConn.setRequestProperty("Content-Length", String.valueOf(sendMsg.getBytes("UTF-8").length));

		httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
		httpConn.setRequestProperty("SOAPAction", "G_GET_LOCATION.v1");

		httpConn.setRequestMethod("POST");
		httpConn.setDoOutput(true);
		httpConn.setDoInput(true);

		// 进行HTTP请求
		OutputStream outObject = httpConn.getOutputStream();
		outObject.write(sendMsg.getBytes("UTF-8"));

		// 关闭输出流
		outObject.close();

		// 获取HTTP响应数据
		InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(), "utf-8");
		BufferedReader inReader = new BufferedReader(isr);
		StringBuffer result = new StringBuffer();
		String inputLine;
		while ((inputLine = inReader.readLine()) != null) {
			result.append(inputLine);
		}

		// 打印HTTP响应数据
		System.out.println(result);

		// 关闭输入流
		inReader.close();
		isr.close();
	}

	// 生成随机数
	private static String getFixLenthString() {

		String result = "";
		for (int i = 0; i < 6; i++) {
			Random rm = new Random();
			double pross = (1 + rm.nextDouble()) * Math.pow(10, 6);
			String fixLenthString = String.valueOf(pross);

			result = result + fixLenthString;
		}

		result = result.replace(".", "").substring(0, 32);
		return result;
	}

	// 测试主方法
	public static void main(String[] args) throws IOException, Exception {
		testHelloService("jack");
	}
}
